import Dialog from '../../../lib/vant-weapp/dialog/dialog';

var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
	
  data: {
		code: '',
		show: false,
		info: {
			name: '',
			money: ''
		}
  },

  onLoad: function (options) {

  },

  onReady: function () {

  },

  onShow: function () {

  },

  onHide: function () {

  },

  onUnload: function () {

  },
	
	// 设置礼品卡密码
	changeVal(e) {
		this.setData({
			code: e.detail.value
		})
	},
	// 绑定查询
	submit() {
		let that = this
		let code = this.data.code.trim()
		if (code && code != '') {
			util.request(api.GetGiftBalanceCard, {
				code: code
			}, 'GET').then(res => {
				if (res.errno == '0') { // 密码存在，且未兑换
					that.setData({
						info: res.data,
						show: true
					})
				} else { // 密码存在问题，如已兑换、不存在
					Dialog.alert({
						message: res.errmsg,
						theme: 'round-button',
					}).then(() => {})
				}
			})
		} else {
			wx.showToast({
				title: '消费扶贫卡密码不能为空',
				icon: 'none'
			})
		}
	},
	// 确定绑定
	add() {
		util.request(api.AddGiftBalanceCard, {
      code: this.data.code
    }, 'POST').then(res => {
			if (res.errno == '0') { // 绑定成功
				wx.showToast({
					title: '绑定成功',
					icon: 'success'
				})
			} else { // 绑定失败
				console.log(res)
				wx.showToast({
					title: res.errmsg,
					icon: 'none'
				})
			}
			this.setData({
				show: false
			})
		})
	},
	// 关闭pop
	onClose() {
		this.setData({
			show: false
		})
	}
})